package de.homelabs.bookmarkmanager.desktop.qt.view;

import com.trolltech.qt.core.QModelIndex;
import com.trolltech.qt.gui.QContextMenuEvent;
import com.trolltech.qt.gui.QTreeView;
import com.trolltech.qt.gui.QWidget;

import de.homelabs.bookmarkmanager.desktop.qt.domain.HBookmarkTreeModel;
import de.homelabs.bookmarkmanager.desktop.qt.menu.HContextMenu;
import de.homelabs.bookmarkmanager.persistence.domain.HBookmark;
import de.homelabs.bookmarkmanager.service.IHContextMenuHandler;

public class HTreeView extends QTreeView {

	private IHContextMenuHandler contextMenuHandler;
	
	public HTreeView(QWidget parent){
		super(parent);
		this.activated.connect(this, "onActivated(QModelIndex)");
		this.clicked.connect(this,"onClicked(QModelIndex)");
	}

	@Override
	protected void contextMenuEvent(QContextMenuEvent cme) {
		super.contextMenuEvent(cme);
		HContextMenu contextMenu = new HContextMenu(this);
		//add bookmarkService handler method
		contextMenu.menuActionSignal.connect(contextMenuHandler, "contextMenuHandler(HAction)");
		//call menu
		contextMenu.exec(cme.globalPos());
	}

	/********************
	 * events			*
	 ********************/
	protected void onActivated(QModelIndex mIndex){
		System.out.println("onActivated: "+mIndex);
	}
	
	protected void onClicked(QModelIndex mIndex){
		System.out.println("onClicked: "+mIndex);
		HBookmarkTreeModel bmt = (HBookmarkTreeModel) mIndex.model();
		HBookmark bookmark = (HBookmark) bmt.indexToValue(mIndex);
		System.out.println(bookmark.getType()+":"+bookmark.getTitle()+","+bookmark.getLink().toString());
	}
	
	/*******************
	 * getter / setter *
	 *******************/
	public IHContextMenuHandler getContextMenuHandler() {
		return contextMenuHandler;
	}

	public void setContextMenuHandler(IHContextMenuHandler contextMenuHandler) {
		this.contextMenuHandler = contextMenuHandler;
	}
	
	
}
